﻿Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Data
Imports System.Data.SqlClient
Imports DoAnQuanLyBanHang1065043_1065007.DoAnQuanLyBanHang.General
Namespace DoAnQuanLyBanHang.DTO
    Public Class DatHangData
        Private m_DatHangData As New DataService()
        Private m_MatHangData As New DataService()
        Public Function LayMatHang() As DataTable
            Dim cmd As New SqlCommand("SELECT * FROM Mat_Hang")
            m_MatHangData.Load(cmd)
            Return m_MatHangData
        End Function
        Public Function LayDatHang() As DataTable
            Dim cmd As New SqlCommand("SELECT * FROM Dat_Hang")

            m_DatHangData.Load(cmd)
            Return m_DatHangData
        End Function
        Public Function LayDSDatHangTheoMa(MaPhieu As [String]) As DataTable
            Dim cmd As New SqlCommand("SELECT DH.MaPhieu, MH.MaMH, KH.MaKH, KH.HoTen, MH.TenMH, LT.LoaiTien, LGD.TenLoaiGiaoDich, DH.SoLuong, DH.GiaBan, DH.ThanhTien, DH.ThanhToan, DH.NgayDat  FROM Dat_Hang DH INNER JOIN Mat_Hang MH ON MH.MaMH = DH.MaMH INNER JOIN Khach_Hang KH ON KH.MaKH = DH.MaKH INNER JOIN Loai_Tien LT ON LT.MaLoaiTien = DH.MaLoaiTien INNER JOIN Loai_Giao_Dich LGD ON LGD.MaLoaiGiaoDich = DH.MaLoaiGiaoDich WHERE DH.MaPhieu = @MaPhieu")
            cmd.Parameters.Add("MaPhieu", SqlDbType.VarChar).Value = MaPhieu
            m_DatHangData.Load(cmd)
            Return m_DatHangData
        End Function

        Public Function ThemDongMoi() As DataRow
            Return m_DatHangData.NewRow()
        End Function

        Public Sub ThemDatHang(m_Row As DataRow)
            m_DatHangData.Rows.Add(m_Row)
        End Sub

        Public Function LuuDatHang() As Boolean
            Return m_DatHangData.ExecuteNoneQuery() > 0
        End Function

        Public Sub LuuDatHang(maphieu As [String], mamh As [String], makh As [String], soluong As Single, giaban As Single, thanhtien As Single, _
         ngaydat As DateTime, thanhtoan As Boolean, maloaitien As [String], maloaigd As [String], ghichu As [String])
            Dim cmd As New SqlCommand("INSERT INTO Dat_Hang VALUES(@maphieu, @mamh, @makh, @soluong, @giaban, @thanhtien, @ngaydat, @thanhtoan, @maloaitien, @maloaigd, @ghichu)")
            cmd.Parameters.Add("maphieu", SqlDbType.VarChar).Value = maphieu
            cmd.Parameters.Add("mamh", SqlDbType.VarChar).Value = mamh
            cmd.Parameters.Add("makh", SqlDbType.VarChar).Value = makh
            cmd.Parameters.Add("soluong", SqlDbType.Float).Value = soluong
            cmd.Parameters.Add("giaban", SqlDbType.Float).Value = giaban
            cmd.Parameters.Add("thanhtien", SqlDbType.Float).Value = thanhtien
            cmd.Parameters.Add("ngaydat", SqlDbType.DateTime).Value = ngaydat
            cmd.Parameters.Add("thanhtoan", SqlDbType.Bit).Value = thanhtoan
            cmd.Parameters.Add("maloaitien", SqlDbType.VarChar).Value = maloaitien
            cmd.Parameters.Add("maloaigd", SqlDbType.VarChar).Value = maloaigd
            cmd.Parameters.Add("ghichu", SqlDbType.NVarChar).Value = ghichu
            m_DatHangData.Load(cmd)
        End Sub

        Public Sub SuaDatHang(maphieu As [String], mamh As [String], makh As [String], soluong As Single, giaban As Single, thanhtien As Single, _
         ngaydat As DateTime, thanhtoan As Boolean, maloaitien As [String], maloaigd As [String], ghichu As [String])
            Dim cmd As New SqlCommand("UPDATE Dat_Hang SET soluong = @soluong, giaban = @giaban, thanhtien = @thanhtien, ngaydat = @ngaydat, thanhtoan = @thanhtoan, maloaitien = @maloaitien, maloaigiaodich = @maloaigd, ghichu = @ghichu WHERE maphieu = @maphieu AND mamh = @mamh AND makh = @makh")
            cmd.Parameters.Add("maphieu", SqlDbType.VarChar).Value = maphieu
            cmd.Parameters.Add("mamh", SqlDbType.VarChar).Value = mamh
            cmd.Parameters.Add("makh", SqlDbType.VarChar).Value = makh
            cmd.Parameters.Add("soluong", SqlDbType.Float).Value = soluong
            cmd.Parameters.Add("giaban", SqlDbType.Float).Value = giaban
            cmd.Parameters.Add("thanhtien", SqlDbType.Float).Value = thanhtien
            cmd.Parameters.Add("ngaydat", SqlDbType.DateTime).Value = ngaydat
            cmd.Parameters.Add("thanhtoan", SqlDbType.Bit).Value = thanhtoan
            cmd.Parameters.Add("maloaitien", SqlDbType.VarChar).Value = maloaitien
            cmd.Parameters.Add("maloaigd", SqlDbType.VarChar).Value = maloaigd
            cmd.Parameters.Add("ghichu", SqlDbType.NVarChar).Value = ghichu
            m_DatHangData.Load(cmd)
        End Sub

        Public Sub XoaDatHang(maphieu As [String], mamh As [String], makh As [String])
            Dim cmd As New SqlCommand("DELETE FROM Dat_Hang WHERE maphieu = @maphieu AND mamh = @mamh AND makh = @makh")
            cmd.Parameters.Add("maphieu", SqlDbType.VarChar).Value = maphieu
            cmd.Parameters.Add("mamh", SqlDbType.VarChar).Value = mamh
            cmd.Parameters.Add("makh", SqlDbType.VarChar).Value = makh
            m_DatHangData.Load(cmd)
        End Sub
    End Class
End Namespace
